﻿
var crypto = require('crypto');
var fs = require('fs');
var privatePem = fs.readFileSync('server.pem');
var publicPem = fs.readFileSync('cert.pem');

function content(c) {
    var key;
    if (c == "public")
        key = publicPem.toString();
    else if (c == "private")
        key = privatePem.toString();
    return key;
}

//认证,注意并非加密
function encode_rsa_sign(data) {
    var sign = crypto.createSign('RSA-SHA256');
    sign.update(data);
    var sig = sign.sign(content("private"), 'hex');
    return sig;
}

function decode_rsa_verify(data, sig) {
    var verify = crypto.createVerify('RSA-SHA256');
    verify.update(data);
    return verify.verify(content("public"), sig, 'hex');
}
//function encode_1(text) {
//    var cipher = crypto.createCipher('aes192', '123');
//    var crypted = cipher.update(text, 'utf8', 'hex')
//    crypted += cipher.final('hex')
//    return crypted;
//}
//function decode_1(crypted) {
//    var decipher = crypto.createDecipher('aes192', '123');
//    var dec = decipher.update(crypted, 'hex', 'utf8')
//    dec += decipher.final('utf8')
//    return dec;
//}




/**
 * 加密方法
 * @param data     需要加密的数据
 * @returns string
 */
var encrypt = function (data) {
    var key = "www.sxzd365.com_"// content("public");
    key = key.toString('hex');
    var iv = "1234567890abcdef";
    var cipher = crypto.createCipheriv('aes-128-cbc', key, iv);
    var crypted = cipher.update(data, 'utf8', 'binary');
    crypted += cipher.final('binary');
    crypted = new Buffer(crypted, 'binary').toString('base64');
    return crypted;
};

/**
 * 解密方法
 * @param crypted  密文
 * @returns string
 */
var decrypt = function (crypted) {
    var key = "www.sxzd365.com_";//content("public");
    key = key.toString('hex');
    var iv = "1234567890abcdef";
    crypted = new Buffer(crypted, 'base64').toString('binary');
    var decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
    var decoded = decipher.update(crypted, 'binary', 'utf8');
    decoded += decipher.final('utf8');
    return decoded;
};


//var test = crypto.privateEncrypt(privatePem.toString(), buffer.from("qianbo"));
//console.log(test);
//crypto.privateEncrypt
var text_connect = {
    host: '118.190.203.202',
    user: 'root',
    password: 'root',
    database: 'construction',
    port: '3306',
    timezone: '+08:00'
}
var local_connect ={
    host:'localhost',
    user:'root',
    password:'root',
    database:'construction',
    port:'3306',
    timezone: '+08:00'
};


function encode_Connect(text) {
    var encrypt_ret = encrypt(text);
    var sig = encode_rsa_sign(encrypt_ret);
    return { encrypt: encrypt_ret, sig: sig };
}
//sig 签名
function decode_Connect(encrypt_str,sig) {
    var c = null;
    var ret = decode_rsa_verify(encrypt_str, sig);
    if (ret) {
        c = decrypt(encrypt_str);
    }
    return c;
}
var url_test = "http://127.0.0.1:9069/sensor_data/8051";


var text = JSON.stringify(url_test);
//console.log(text);
var obj = encode_Connect(url_test);
console.log(obj);
var ret = decode_Connect(obj.encrypt, obj.sig);
//console.log(ret);
var dd = JSON.parse(ret);
console.log(dd);
